//app.js
App({
  config: require('utils/config.js'), //全局配置信息
  api: require('utils/api.js'), //接口文档

  common: require('utils/common.js'),
  globalData: {
    userInfo: null
  },
  onLaunch: function(e) {
    var that = this;
    var pages = this;

    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        //获取code
        success: function(res) { 
          code = res.code //返回code 
          wx.getUserInfo({
            success: function(res) {
              userInfo = {
                encryptedData: res.encryptedData,
                iv: res.iv,
                code: code
              }; 
              var data = {
                code: userInfo,
                method: 'user.wxapplogin1'
              };
              wx.request({
                url: that.config.api_url + 'law.html',//未开启伪静态时，请把api改成index.php/api
                data: data,
                method: 'POST',
                success: function (res) {
                  console.log(res.data) 
                  that.globalData.userInfo = res.data
                },
              })  
              typeof cb == "function" && cb(that.globalData.userInfo)
            } 
          })
        }
      })
    }
  },

  getUserInfo: function(cb) {
    var that = this 
    if (that.globalData.userInfo) {
      typeof cb == "function" && cb(that.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        //获取code
        success: function(res) {
          code = res.code //返回code 
          wx.getUserInfo({
            success: function(res) { 
              cc = {
                encryptedData: res.encryptedData,
                iv: res.iv,
                code: code
              }; 
              var data = {
                code: cc,
                method: 'user.wxapplogin1'
              };
              wx.request({
                url: that.config.api_url + 'law.html',//未开启伪静态时，请把api改成index.php/api
                data: data,
                method: 'POST',
                success: function (res) { 
                  that.globalData.userInfo = res.data
                },
              })   
              typeof cb == "function" && cb(that.globalData.userInfo)
            }

          })
        }
      })
    }
  }
})